<template>
  <div>
    <template>
      <!-- 新增部门的弹层 -->
      <el-dialog :title="titleTip" :visible="showDialog" @close="btnCancel">
        <!-- 表单组件  el-form   label-width设置label的宽度   -->
        <!-- 匿名插槽 -->
        <el-form ref="deptForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item prop="name" label="部门名称">
            <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item prop="code" label="部门编码">
            <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item prop="manager" label="部门负责人">
            <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
              <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username" />
            </el-select>
          </el-form-item>
          <el-form-item prop="introduce" label="部门介绍">
            <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
          </el-form-item>
        </el-form>
        <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
        <el-row slot="footer" type="flex" justify="center">
          <!-- 列被分为24 -->
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnOk">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  name: '',
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    // 现在定义一个函数 这个函数的目的是 去找 同级部门下 是否有重复的部门名称
    const checkName = async(rules, value, callback) => {
      // 找到当前点击的部门的所有子部门
      // 请求所有数据
      const { depts } = await getDepartments()
      // 判断是编辑还是新增 根据有无formdata.id
      let isRepeat = false
      if (this.formData.id) {
        // 去编辑 同级部门下除了自己 名字不能重复
        isRepeat = depts.filter(item => item.id !== this.treeNode.id && item.pid === this.treeNode.pid).some(item => item.name === value)
      } else {
        // 去新增
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }
      // 同级部门的名称不能相同
      // treeNode当前点击的节点
      // some方法 只要有一个条件满足就返回true 查看是否有满足条件的元素
      isRepeat ? callback(new Error(`${value}已存在`)) : callback()
    }
    const checkCode = async(rules, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.formData.id) {
        isRepeat = depts.some(item => item.id !== this.treeNode.id && item.code === value)
      } else {
        isRepeat = depts.some(item => item.code === value)
      }
      isRepeat ? callback(new Error(`${value}已存在`)) : callback()
    }
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkName }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCode }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '1-300个字符', trigger: 'blur' }]
      },
      peoples: []
    }
  },
  computed: {
    titleTip() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  watch: {},
  created() {},
  methods: {
    // 获取员工简单列表数据
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    },
    // 获取部门详情
    async getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    },
    btnOk() {
      this.$refs.deptForm.validate(async isOk => {
        if (isOk) {
          // 有id 编辑部门 没id新增部门
          if (this.formData.id) {
            await updateDepartments(this.formData)
            // 提示
            this.$message.success('编辑成功')
          } else {
            await addDepartments({ ...this.formData, pid: this.treeNode.id })
            this.$message.success('添加成功')
          }

          this.$emit('addDepts')
          this.$emit('update:showDialog', false)
        }
      })
    },
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false) // 关闭
    }
  }
}
</script>

<style scoped>

</style>
